package test.sunyu.service.hbase;

import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sunyu.service.hbase.HbaseService;
import sunyu.tools.common.FileTools;

import java.io.IOException;
import java.util.List;

public class CountTask2 implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());

    List<String> didList;
    String filePath;
    FileTools fileTools;
    HbaseService hbaseService;

    public CountTask2(List<String> didList,
                      String filePath,
                      FileTools fileTools,
                      HbaseService hbaseService) {
        this.didList = didList;
        this.filePath = filePath;
        this.fileTools = fileTools;
        this.hbaseService = hbaseService;
    }

    @Override
    public void run() {
        for (String did : didList) {
            System.out.println(Thread.currentThread().getName() + ":处理" + did);
            FilterList filterList = new FilterList();
            Filter preFilter = new PrefixFilter(Bytes.toBytes(did));
            filterList.addFilter(preFilter);
            try {
                String msg = did + ":" + hbaseService.count("bc_msg", did, null, null, filterList);
                System.out.println(msg);
                fileTools.appendContent(filePath, msg, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
